package com.luxsoft.siipap.em.replica.ui.tasks;

import java.util.List;

import javax.swing.SwingWorker;

import com.luxsoft.siipap.em.replica.importadores.inventarios.ImportadorDeInventarioAnual;
import com.luxsoft.siipap.service.ServiceLocator2;

/**
 * Componente grafico para la importacion de clientes
 * 
 * @author Ruben Cancino
 *
 */
public class ImportadorDeInventarioInicialTask {

	public ImportadorDeInventarioInicialTask() {
		
	}
	
	public void execute(){		
		TaskDialog dialog=new TaskDialog("Importar inventario inicial");
		final SwingWorker worker=new Importador(dialog);
		dialog.registerWorker(worker);
		dialog.open();
		printResult();
	}
	
	protected void printResult(){
		
	}
	
	
	private class Importador extends SwingWorker<String,String>{
		
		private final TaskDialog dialog;
		
		public Importador(final TaskDialog dialog){
			this.dialog=dialog;
		}

		@Override
		protected String doInBackground() throws Exception {
			System.out.println("Iniciando tarea");
			final int total=ServiceLocator2.getAnalisisJdbcTemplate()
				.queryForInt("select count(*) from V_INI_2009");
			dialog.getHeader().setDescription("Total de registros ini a importaro: "+total);
			ImportadorDeInventarioAnual importer=new ImportadorDeInventarioAnual(){

				@Override
				public void setProgress(int progress){
					updateProgress(progress);
				}
				
			};
			importer.importar(2009);
			return "OK";
		}
		
		private void updateProgress(int p){
			setProgress(p);
		}

		@Override
		protected void process(List<String> chunks) {
			for(String s:chunks){
				dialog.infoArea.append("\n"+s);
			}
		}

		
		
	}
	
	public static void main(String[] args) {		
		new ImportadorDeInventarioInicialTask().execute();
	}

}
